package game;

import game.maps.Brick;


import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.FileInputStream;

import java.io.*;
import java.util.List;

import sun.audio.*;

public class KeyboardControls extends KeyAdapter {
	
	private Model model;
	
	private Movement movement;

	private boolean left = false;
	
	private boolean right = false;
	
	private boolean up = false;
	
	private boolean down = false;
	
	public KeyboardControls(Model model, Movement movement) {
		this.model = model;
		this.movement = movement;
	}
		
	
	@Override
	public void keyPressed(KeyEvent e) {
		Player me = model.getMyPlayer();

		int keyCode = e.getKeyCode();			

		if (keyCode == 37) {
			left = true;
			me.setHorizontalDirection(Direction.LEFT);
		}
		else if (keyCode == 39) {
			right = true;
			me.setHorizontalDirection(Direction.RIGHT);
		}
		else if (keyCode == 38) {
			up = true;
			me.setVerticalDirection(Direction.UP); 
		}
		else if (keyCode == 40) {
			down = true;
			me.setVerticalDirection(Direction.DOWN);
		}
		/*
		else if (keyCode == 65){
			me.setCoordinates(me.getX() - 100, me.getY());
			
			try {
			
				InputStream in = new FileInputStream("sounds/jump.wav");				
				AudioStream as = new AudioStream(in);   
				AudioPlayer.player.start(as);  
	
			} catch (Exception e1) { 
				e1.printStackTrace();
			}
		}			
		
		else if (keyCode == 68){
			me.setCoordinates(me.getX() + 100, me.getY());	
			
			try {
				
				InputStream in = new FileInputStream("sounds/jump.wav");				
				AudioStream as = new AudioStream(in);   
				AudioPlayer.player.start(as);  
	
			} catch (Exception e1) { 
				e1.printStackTrace();
			}
			
		}
		*/
		else if (keyCode == KeyEvent.VK_SPACE){			
			
			try {
				
				InputStream in = new FileInputStream("sounds/pop.wav");				
				AudioStream as = new AudioStream(in);   
				AudioPlayer.player.start(as);  
	
			} catch (Exception e1) { 
				e1.printStackTrace();
			}
			
			final Bullet bullet = Bullet.createBulletFor(me);
			bullet.addCollisionListener(new CollisionListener() {				
				@Override
				public void collision(List<Collision> collisions) {
					for (Collision c : collisions) {
						if (c.getObject() instanceof Player) {
							if (c.getObject()!=model.getMyPlayer()){
								model.getEnemyPlayer().setLife(model.getEnemyPlayer().getLife()+1);
								try {
									
									InputStream in = new FileInputStream("sounds/uu.wav");				
									AudioStream as = new AudioStream(in);   
									AudioPlayer.player.start(as);  
						
								} catch (Exception e1) { 
									e1.printStackTrace();
								}
								System.out.println("YOU HIT THE PLAYER");
							}
							
						}
					}
					model.getSprites().remove(bullet);
					movement.remove(bullet);
				}
			}); 
			model.getSprites().add(bullet);
			movement.registerForMovement(bullet);
		}
	}
	
	@Override
	public void keyReleased(KeyEvent e) {
		int keyCode = e.getKeyCode();

		Player me = model.getMyPlayer();
		Direction d;
		
		if (keyCode == 37) {
			left = false;
			d = Direction.NONE;
			if (right) d = Direction.RIGHT;
			me.setHorizontalDirection(d);
		}
		else if (keyCode == 39) {
			right = false;
			d = Direction.NONE;
			if (left) d = Direction.LEFT;
			me.setHorizontalDirection(d);
		}
		else if (keyCode == 38) {
			up = false;
			d = Direction.NONE;
			if (down) d = Direction.DOWN;
			me.setVerticalDirection(d);
		}
		else if (keyCode == 40) {
			down = false;
			d = Direction.NONE;
			if (up) d = Direction.UP;
			me.setVerticalDirection(d);
		}
		
	}
	
	
	
}
